package com.young.shop_admin.controller;


import com.young.shop_admin.pojo.entity.Goods;
import com.young.shop_admin.services.GoodsService;
import com.young.shop_admin.services.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *  商品搜索
 */
@WebServlet("/selectGoodsByType")
public class SelectGoodsByTypeController extends BaseController{

    GoodsService goodsServiceImpl = new GoodsServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //跨域
        resp.setHeader("Access-Control-Allow-Origin","http://localhost:63342");

        String searchByType = req.getParameter("searchByType");
        System.out.println(searchByType);
        if (searchByType == null) {
            List<Goods> allGoods = goodsServiceImpl.getAllGoods();
            writerSuccessResult(allGoods,resp);
            return;
        }


        List<Goods> goodsList = goodsServiceImpl.selectGoodsListByType(searchByType);
        System.out.println(searchByType);
        writerSuccessResult(goodsList,resp);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
